package com.bishe.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bishe.pojo.FileInfo;
import com.bishe.service.IFileService;
import com.bishe.util.EcoBootException;
import com.bishe.util.JobUtils;
import com.bishe.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * 文件控制器
 * @date 2022/2/16 16:59
 */
@RestController
@CrossOrigin
@RequestMapping("/file")
@Api(tags = "文件控制器")
public class FileController {

    @Autowired
    private IFileService fileService;

    @ApiOperation("生成验证码")
    @GetMapping("/getImg")
    public void getImg(HttpServletResponse response){
        JobUtils.createImgCode(response);
    }

    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public Result upload(MultipartFile file, String dataId){
        fileService.upload(file,dataId);

        return Result.ok("上传成功!");

    }


    @ApiOperation("下载文件")
    @GetMapping("/download/{fileId}")
    public Result download(@PathVariable String fileId, HttpServletResponse response){
        FileInfo fileInfo = fileService.getById(fileId);
        //下载文件
        JobUtils.download(response,JobUtils.getFile(fileInfo));
        return Result.ok();
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/delete/{id}")
    public Result deleteFileById(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        fileService.deleteFileById(id);
        return Result.ok();
    }


    @ApiOperation("删除文件")
    @DeleteMapping("/deleteFile/{fileId}")
    public Result deleteFile(@PathVariable String fileId){
        if (StrUtil.isEmpty(fileId)){
            throw new EcoBootException("id不能为空");
        }
       fileService.deleteFile(fileId);
        return Result.ok();
    }

    @ApiOperation("获取头像")
    @GetMapping("/getPhoto/{id}")
    public Result getPhoto(HttpServletResponse response,@PathVariable String id){
        FileInfo fileInfo = fileService.getByDateId(id);
        if (fileInfo != null){
            JobUtils.download(response,JobUtils.getFile(fileInfo));
            return Result.ok();
        }
        return null;
    }

    @ApiOperation("根据创建人获取文件列表")
    @GetMapping("/getFiles/{id}")
    public Result getFiles(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id为空");
        }

        List<FileInfo> files = fileService.getFiles(id);

        return Result.ok(files);
    }



    @ApiOperation("查询")
    @PostMapping("/list")
    public Result list(){
        //检查登录
        JobUtils.checkLogin();

        List<FileInfo> list = fileService.list();

        return Result.ok(list);
    }

}
